<?php
session_start();
require_once '../header.php';
require_once 'array.php';
function __autoload($className)
	{
    	require_once('../class/' . $className . '.class.php');
	}
session::need_member();
$Plan=$_SESSION["plan"];
$UserId=session::getuserid();
$CanBuy=user::need_CanBuy($UserId);
$CanBuy=TRUE;
if($CanBuy==FALSE)
{
	$_SESSION["erro"]="Your deposit is not enough 40$, now you need to deposit enough 40$";
	redirect::location("../orderhistory.php");
	unset($_SESSION["cc"]);
	unset($_SESSION["ccid"]);
	unset($_SESSION["moneyplus"]);

}
else 
{
	
if($Plan=="our")
{
	$MoneyPlus=$_SESSION["moneyplus"];
	$TotalPlus=count($_SESSION["ccid"])*$MoneyPlus;
	$CcIdList=$_SESSION["ccid"];
	$Condition=database::BuildCondition(array(
	'ccid' => $CcIdList
	)
	);
	$MoneyPriceTemp=database::LimitQuery('ccinfo',array(
	'select' => 'sum(price)',
	'condition' => $Condition
	));
	$MoneyPrice=$MoneyPriceTemp[0]['sum(price)']+$TotalPlus;
	$MoneyComplete=round($MoneyPrice,2);
	$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $MoneyComplete");
	if($CheckMoney==1)
	{
		$UpdateMoney=database::Update('userinfo',array(
		'userid' => $UserId
		),array('money' => array( "money - {$MoneyComplete}" ))
		);
		$InsertOrder=user::insertorder($UserId, $MoneyComplete, 'our');
		foreach($_SESSION["cc"] as $GetCcId)
		{
			$CcId=$GetCcId["ccid"];
			$GetPrice=database::LimitQuery('ccinfo',array(
			'select' => 'price',
			'condition' => array(
			'ccid' => $CcId
			)
			));
			$Price=$GetPrice[0]["price"];
			$PriceSell=$Price+$MoneyPlus;
			$UpdateCc=database::Update('ccinfo',array(
			'ccid' => $CcId
			),array(
			'pricesell' => $PriceSell,
			'orderid' => $InsertOrder,
			'userid' => $UserId,
			'site' => 'our'
			));
			
		}
		$_SESSION["erro"]="Thank You, Your order has been completed";
		redirect::location("../orderhistory.php");
		
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		unset($_SESSION["cc"]);
		redirect::location("../lr/deposit.php");
	}
	unset($_SESSION["countrybuy"]);
	
}
else if($Plan=="ltd")
{
	$GetArrPrice=database::LimitQuery('agent_price',array(
		'select' => 'agent_name,typefind,country,agent_price',
		));
		$ArrPrice=array();
	foreach($GetArrPrice as $ShowArrPrice)
		{
			$Key=$ShowArrPrice['agent_name'].$ShowArrPrice['typefind'].$ShowArrPrice['country'];
			$ArrPrice[$Key]=$ShowArrPrice['agent_price'];
		}
	$MoneyPlus=$_SESSION["moneyplus"];
	$TotalPlus=count($_SESSION["ccid"])*$MoneyPlus;
	$CcIdList=$_SESSION["ccid"];
	$Quality=count($CcIdList);
	$Plan=$_SESSION["plan"];
	$TotalPlus=count($_SESSION["ccid"])*$MoneyPlus;
	$Money=0;
	$ListPrice=NULL;
	for($i=0;$i<count($CcIdList);$i++)
	{
		$CcId=$CcIdList[$i];
		$CcNumber=$_SESSION["cc"][$CcId]["ccnum"];
		$TypeFind=substr($CcNumber,0,1);
		$Country=$_SESSION["cc"][$CcId]['country'];
		$Key=$Plan.$TypeFind.$Country;
		$_SESSION["country"]=$Country;
		if(empty($ArrPrice["$Key"]))
		{
			$Price=15;
		}
		else 
		{
			$Price=$ArrPrice[$Key];
		}
		$PriceS=$MoneyPlus+$Price;
		$ListPrice=$CcNumber."|".$PriceS."/".$ListPrice;
		$Money=$Price+$Money;
	}
	$TotalMoney=$Money+$TotalPlus;
	$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $TotalMoney");
	if($CheckMoney==1)
	{
		$UpdateMoney=database::Update('userinfo',array(
		'userid' => $UserId
		),array('money' => array( "money - {$TotalMoney}" ))
		);
		$InsertOrder=user::insertorder($UserId, $TotalMoney, $Plan);
		$BuyCc=ltdcc::CheckOut($CcIdList);
		if($BuyCc==FALSE)
		{
			$_SESSION["erro"]="Our System has some problem, plz choose another plan";
			redirect::location("buycc.php");
		}
		else 
		{
			
			$Country=$_SESSION["country"];
			$Orderid=buycc::GetMaxId("orders","orderid", "userid=$UserId");
			$GetCc=ltdcc::GetCC($Quality, $ListPrice, $UserId, $Orderid,$Country);
			$_SESSION["erro"]="Thank You, Your order has been completed";
			redirect::location("../orderhistory.php");
		}
		
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		unset($_SESSION["cc"]);
		redirect::location("../lr/deposit.php");
	}
	unset($_SESSION["countrybuy"]);
	
}
else if($Plan=="codesell")
{
	echo "<img src='../images/loading.gif' />";
	$post=$_POST;
	$country=$_SESSION["countrybuy"];
	$moneyPlus=$_SESSION["moneyplus"];
	$price=$_SESSION["pricecodesell"];
	$quatity=rtrim(trim($post["freshcvvquatity"]));
	$quatity=intval($quatity);
	$TotalMoney=$quatity*$price;
	$typeCc=$_SESSION["typecc"];
	$binCc=$_SESSION["bincc"];
	$dob=$_SESSION["dob"];
	$getBalance=codesell::balanceCodeSell();
	if($TotalMoney>$getBalance)
	{
		$_SESSION["erro"]="Our System has some problem, plz choose another plan";
		redirect::location("../membercp.php");
	}
	else
	{
		$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $TotalMoney");
		if($CheckMoney==1)
		{
			$UpdateMoney=database::Update('userinfo',array(
			'userid' => $UserId
			),array('money' => array( "money - {$TotalMoney}" ))
			);
			$InsertOrder=user::insertorder($UserId, $TotalMoney, $Plan);
			$Orderid=buycc::GetMaxId("orders","orderid", "userid=$UserId");
			$searchCc=codesell::checkoutCodesell($typeCc, $country, $binCc, $dob, $quatity, $UserId, $Orderid, $price);
			if($searchCc=="erro")
			{
				$_SESSION["erro"]="Our System has some problem, plz choose another plan";
				redirect::location("../membercp.php");
			}
			else
			{
				$_SESSION["erro"]="Thank You, Your order has been completed";
				redirect::location("../orderhistory.php");
			}
		}
		
	
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		redirect::location("../lr/deposit.php");
		unset($_SESSION["countrybuy"]);
		unset($_SESSION["moneyplus"]);
		unset($_SESSION["pricecodesell"]);
	}
	}
	
}
}
?>